package com.kongmou.Springmvc.config;

import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

import com.kongmou.Springmvc.httpMessageConvert.MyMessageConvert;
import com.kongmou.Springmvc.interceptor.DemoInterceptor;

@Configuration
@ComponentScan({ "com.kongmou.Springmvc" })
@EnableWebMvc
@EnableScheduling // 开启定时任务的支持
public class MyMvcConfig implements WebMvcConfigurer {

	@Bean
	public InternalResourceViewResolver viewResolver() {
		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
		viewResolver.setPrefix("/");
		viewResolver.setSuffix(".jsp");
		viewResolver.setViewClass(JstlView.class);
		return viewResolver;
	}

	// 添加对静态资源的访问
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {

		registry.addResourceHandler("/assets/**").addResourceLocations("/assets/");// 3

	}

	// 拦截器
	@Bean
	public DemoInterceptor demoInterceptor() {
		return new DemoInterceptor();
	}

	// 开启拦截器
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(demoInterceptor());
	}

	// viewController
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		// 上传文件
		registry.addViewController("/toupload").setViewName("/upload");
		// httpMessageConvert
		registry.addViewController("/convert").setViewName("convert");
		// SSE测试页面
		registry.addViewController("/sse").setViewName("/sse");
		// servlet3.0+异步处理方式
		registry.addViewController("/async").setViewName("/async");
	}

	// multipartServlet文件上传
	@Bean
	public MultipartResolver multipartResolver() {
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
		multipartResolver.setMaxUploadSize(1000000000);
		return multipartResolver;
	}

	// 注册HttpMesasgeConvert
	// 重载configureMessageConverters()会覆盖springMvc已注册的转换器
	// 自定义extendMessageConverters()不会覆盖
	@Override
	public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
		converters.add(myMessageConvert());
	}

	@Bean
	public MyMessageConvert myMessageConvert() {
		return new MyMessageConvert();
	}
}
